<?php

declare (strict_types=1);
namespace App\Model;

/**
 * @property int $id 
 * @property string $title 
 * @property string $icon 
 * @property int $type 
 * @property int $parent_id
 * @property string $component 
 * @property string $path
 * @property string $module 
 * @property string $controller 
 * @property string $method 
 * @property int $sort 
 * @property string $target 
 * @property int $status 
 * @property \Carbon\Carbon $create_time 
 * @property \Carbon\Carbon $update_time 
 * @property int $delete_time 
 */
class Menu extends Model
{
    protected $table = 'menu';

    protected $primaryKey = 'id';

    public $timestamps = true;

    protected $fillable = [
        'id',
        'title',
        'icon',
        'type',
        'parent_id',
        'action',
        'component',
        'module',
        'controller',
        'method',
        'path',
        'sort',
        'status'
    ];
    protected $casts = [
        'id' => 'integer',
        'title' => 'string',
        'icon' => 'string',
        'type' => 'integer',
        'parent_id' => 'integer',
        'action' => 'string',
        'component' => 'string',
        'module' => 'string',
        'controller' => 'string',
        'method' => 'string',
        'path' => 'string',
        'sort' => 'integer',
        'status' => 'integer',
        'create_time' => 'datetime',
        'update_time' => 'datetime',
        'delete_time' => 'integer'];

    /**
     * 取菜单列表，最多为三级
     * @param int $showDeep 显示深度
     * @param array $condition 检索条件
     * @param string $field 检索字段
     * @return array 数组类型的返回结果
     */
    public static function getMenuTreeList($data,$showDeep = 3,$field ='*')
    {
        //获取所有的菜单
        $classList = self::query()
            ->orderBy('parent_id','asc')
            ->orderBy('sort','desc')
            ->orderBy('id','asc')
            ->select('id','title','icon','type','parent_id','component','path','action','sort','status')
            ->get()
            ->toArray();
        $goodsClass = [];//分类数组
        if (is_array($classList) && !empty($classList)) {
            $showDeep = intval($showDeep);
            if ($showDeep == 1) {//只显示第一级时用循环给分类加上深度deep号码
                foreach ($classList as $val) {
                    if ($val['parent_id'] == 0) {
                        $val['deep'] = 1;
                        $goodsClass[] = $val;
                    } else {
                        break;//父类编号不为0时退出循环
                    }
                }
            } else {//显示第二和三级时用递归
                $goodsClass = self::getTreeClassList($showDeep, $classList);
            }
        }
        return $goodsClass;
    }

    /**
     * 递归 整理分类
     * @param int $show_deep 显示深度
     * @param array $class_list 类别内容集合
     * @param int $deep 深度
     * @param int $parent_id 父类编号
     * @param int $i 上次循环编号
     * @return array $show_class 返回数组形式的查询结果
     */
    public static function getTreeClassList($showDeep, $classList, $deep = 1, $parentId = 0, $i = 0)
    {
        static $showClass = array();//树状的平行数组
        if (is_array($classList) && !empty($classList)) {
            $size = count($classList);
            if ($i == 0) $showClass = array();//从0开始时清空数组，防止多次调用后出现重复
            for ($i; $i < $size; $i++) {//$i为上次循环到的分类编号，避免重新从第一条开始
                $val = $classList[$i];
                $gcId = $val['id'];
                $gcParentId = $val['parent_id'];
                if ($gcParentId == $parentId) {
                    $val['deep'] = $deep;
                    $showClass[] = $val;
                    if ($deep < $showDeep && $deep < 3) {//本次深度小于显示深度时执行，避免取出的数据无用
                        self::getTreeClassList($showDeep, $classList, $deep + 1, $gcId, $i + 1);
                    }
                }
                if ($gcParentId > $parentId) break;//当前分类的父编号大于本次递归的时退出循环
            }
        }
        return $showClass;
    }
}